package air2.totc.testing;

import java.security.GeneralSecurityException;
import java.security.InvalidKeyException;
import java.security.KeyPair;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;

import air2.totc.common.Configure;
import air2.totc.crypt.*;

public class TestProject 
{
	public static String runTest()
	{
		Configure.configure();
		// Création de la clé
		String desKey = "CLEMACHINQUIVAUTRIEN";
		String msg = "ceci est un long, long, long, long, long, long, long, long, long, long, long, long, méssàgé\n";
		String encMsg;
		String decMsg;
		String out="entrée";
		
		try {
			/*
			// Tests DES
			DESEncoder enc = new DESEncoder(desKey.getBytes());
			encMsg = enc.encode(msg);
			DESEncoder dec = new DESEncoder(desKey.getBytes());
			decMsg = dec.decode(encMsg);
			*/
			
			// On crée deux nouveaux destinataires RSA
			KeyPair me = RSAEncoder.genKeyPair();
			KeyPair you = RSAEncoder.genKeyPair();
			/*
			// Encodage RSA
			RSAEncoder renc0 = new RSAEncoder();
			renc0.setPrvKey(me.getPrivate());
			renc0.setPubKey(you.getPublic());
			
			encMsg = renc0.encode(msg);
			
			 
			// Décodage RSA
			RSAEncoder rdec0 = new RSAEncoder();
			rdec0.setPrvKey(you.getPrivate());
			rdec0.setPubKey(me.getPublic());
			
			decMsg = rdec0.decode(encMsg);*/
			
			/*
			String key = new String("AESPassword");
			
			AESEncoder aes = new AESEncoder(key.getBytes());
			
			encMsg = aes.encode(msg);
			decMsg = aes.decode(encMsg);*/
			
			String key = new String("3DESPassword");
			
			TripleDesEncoder _3des = new TripleDesEncoder(key.getBytes());
			
			encMsg = _3des.encode(msg);
			decMsg = _3des.decode(encMsg);
			
			out = new String("Message \n###\n"+msg+
						"\n###\nMessage crypté:\n###\n"+encMsg+
						"\n###\nMessage décodé\n###\n"+decMsg);
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return e.getMessage();
		} catch (NoSuchPaddingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return e.getMessage();
		} catch (InvalidKeyException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return e.getMessage();
		} catch (IllegalBlockSizeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return e.getMessage();
		} catch (BadPaddingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return e.getMessage();
		} catch (NoSuchProviderException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return e.getMessage();
		} catch (GeneralSecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return e.getMessage();
		}
		return out;
	}
}
